package f_Collectors类.mapping方法的使用;

import z_Entity.User;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 遍历流操作流中元素
 *
 * @author xuyaxu
 * @date 2022/8/27 22:24
 */
public class MappingDemo {
    public static void main(String[] args) {
        List<User> userList = new ArrayList<>();
        userList.add(new User(1, "jack", 18, 0));
        userList.add(new User(2, "rose", 20, 1));
        userList.add(new User(3, "huahua", 2, 0));

        // 收集类中其中一个值拼接为字符串
        String userNameStr = userList.stream().collect(Collectors.mapping(User::getUserName, Collectors.joining(",", "[", "]")));
        // 结果：[jack,rose,huahua]
        System.out.println(userNameStr);

        // 方式二效果同上
        String userNameStr2 = userList.stream().map(user -> user.getUserName()).collect(Collectors.joining(",", "[", "]"));
        // 结果：[jack,rose,huahua]
        System.out.println(userNameStr2);
    }
}
